import unittest

import requests

from api import log
from api.api_register_login import ApiRegisterLogin
from api.api_tender import ApiTender
from utils import parser_html, read_json
from parameterized import parameterized


class TestTender(unittest.TestCase):
    def setUp(self) -> None:
        self.session = requests.Session()
        self.tender = ApiTender(self.session)
        # 调用登录
        ApiRegisterLogin(self.session).api_login()

    def tearDown(self) -> None:
        self.session.close()

    @parameterized.expand(read_json("tender.json", "tender"))
    def test_tender(self, amount, expect_data):
        try:
            # 调用投资
            resp = self.tender.api_tender(amount)
            if amount == 100:
                # 三方投资
                res = parser_html(resp)
                resp = self.session.post(url=res[0], data=res[1])
                print('三方投资结果为:', resp.text)
                log.info('执行接口结果为:{}'.format(resp.text))
                self.assertIn(expect_data, resp.text)
                log.info('断言成功')
            else:
                self.assertIn(expect_data, resp.text)
        except Exception as e:
            log.error(e)
            raise
